import axios from 'axios'

const axiosInstance = axios.create({
  baseURL: 'https://blockseal.network/jsms', // 测试服
  // baseURL: 'https://bjjsms.com.cn/jsms/', // 正式服
  responseType: 'json',
  timeout: 50000
})
axiosInstance.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8'

/**
  * get方法，对应get请求
  *@param {String} url [请求的url地址]
  * @param {Object} params [请求时携带的参数]
*/

export function get (url, reqData) {
  return new Promise((resolve, reject) => {
    axiosInstance.get(url, {
      params: reqData
    }).then(res => {
      resolve(res)
    }).catch(err => {
      reject(err)
    })
  })
}
export function getHeader (url, reqData) {
  return new Promise((resolve, reject) => {
    axiosInstance.get(url, {
      params: reqData
      // headers: {
      //   'X-Auth-Token': token
      // }
    }
    ).then(res => {
      resolve(res)
    }).catch(err => {
      reject(err)
    })
  })
}

export function post (url, reqData) {
  return new Promise((resolve, reject) => {
    axiosInstance({
      method: 'post',
      url,
      data: reqData,
      responseType: 'json'
    }).then(res => {
      resolve(res)
    }).catch(err => {
      reject(err)
    })
  })
}
export function postHeader (url, reqData) {
  return new Promise((resolve, reject) => {
    axiosInstance({
      method: 'post',
      url,
      headers: {
        'X-Auth-Token': JSON.parse(sessionStorage.getItem('authorization')).userToken.token
      },
      data: reqData,
      responseType: 'json'
    }).then(res => {
      resolve(res)
    }).catch(err => {
      reject(err)
    })
  })
}
